/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sembr.vtk.reader;

import vtk.*;
/**
 *
 * @author Adriano
 */
public class VrmlReader extends ReaderBase
{
    private vtkVRMLImporter reader;
    private vtkPolyDataMapper mapper;
    private vtkPolyData polyData;
    
    public VrmlReader(String filePath) 
    {
        //LoadVTKLibraries();
        super.setFilePath(filePath);        
        reader = new vtkVRMLImporter();
        polyData = new vtkPolyData();
        mapper = new vtkPolyDataMapper();
        processDataFromReader();
        setDataSet();       
    }
    
    private void processDataFromReader()
    {
        this.reader.SetFileName(super.getFilePath());
        this.reader.Read();
        //this.reader.Update();

        this.setDataSet();        
    }
    
    private void setDataSet()
    {       
        //reader.GetRenderWindow().Start();
        
        vtkActorCollection actors = reader.GetRenderer().GetActors();
        actors.InitTraversal();
        int i = actors.GetNumberOfItems();
        vtkDataSet dataSet = actors.GetNextActor().GetMapper().GetInputAsDataSet();
        
        this.setPolyData(dataSet);
    }
    
    private void setPolyData(vtkDataSet dataSet)
    {        
        polyData.CopyAttributes(dataSet);
        polyData.CopyStructure(dataSet);
        polyData.Update();
        
        this.setMapper(polyData);
    }
    
    @Override
    public vtkPolyData getPolyData() 
    {
        return polyData;
    }
    
    private void setMapper(vtkPolyData polyData)
    {
        this.mapper.SetInput(polyData);
    }   
}
